package net.tv90.bfmccore.listener;

import net.tv90.bfmccore.battlepack.EnumBattlepackLevel;
import net.tv90.bfmccore.util.HUD.HUD;
import net.tv90.bfmccore.util.HUD.HUDDelayedQueue;
import net.tv90.bfmccore.util.HUD.Text;
import net.tv90.bfmccore.util.vector.Vector2i;
import org.bitterorange.flansmodslain.models.PlayerRankUpEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.ArrayList;
import java.util.List;

import static net.tv90.bfmccore.battlepack.Battlepack.getBattlepack;
import static net.tv90.bfmccore.helper.FactionHelper.getFaction_Lang;

public class RankUpListener implements Listener {
    @EventHandler
    public void onPlayerRankUp(PlayerRankUpEvent e) {
        Player player = e.getPlayer();

        for(int i = 1; i <= e.getRankIndex(); i++){
            if(!player.hasPermission("fms.rank." + i)){
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + player.getName() + " permission set fms.rank." + i);
            }
        }

        Bukkit.broadcastMessage("§e[管理员]玩家" + player.getName() + "(" + getFaction_Lang(player) + ")" + "军衔晋升至" + e.getRank() + "(" + e.getRankIndex() + "级)");

        Text text = new Text(new Vector2i(-1, 120), e.getRank(), 1.0D);
        List<Text> list = new ArrayList<>();
        list.add(text);
        HUD hud = new HUD(player, "[local]RankIcons/" + e.getRankIndex() + ".png", "custom.promotion", list,
                new Vector2i(-1, 25), new Vector2i(100, 100), 5);
        HUDDelayedQueue.addHUD(player, hud);

        if (e.getRankIndex() >= 50 && e.getRankIndex() != 0) {
            getBattlepack(player, EnumBattlepackLevel.GOLD);
        } else {
            if (e.getRankIndex() % 15 == 0) {
                getBattlepack(player, EnumBattlepackLevel.SILVER);
            } else {
                getBattlepack(player, EnumBattlepackLevel.BRONZE);
            }
        }

    }
}
